﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class 药品直入直出单录入 : Form
    {
        public static int rkdh = 0;//入库单号
        public int r0_flag = 0;//入库单flag
        public bool is_new = true;//是否为新单的标志
        public string gysbm ="";//供应商编码
        public int rxh = 0;//入库序号
        public int cxh = 0;//出库序号
        public string ksbm = "";//科室编码

        public 药品直入直出单录入(int dh)
        {
            rkdh = dh;
            InitializeComponent();
            fresh();
            
        }

        public void fresh()//刷新
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                string cmdtext1 = "select  rkdh as 入库单号,xh as 序号,(select  convert(datetime,rq-2)) as 制单日期,CR.MC AS 入库类型,gys.mc as '供应商/退药科室'," +
                                "lrry as 录入,shry as 审核,jzry as 记账,bz 备注, r0.flag as 状态,r0.cgry as 采购人员  FROM YK_RKD0 R0 LEFT JOIN YK_CRLX CR ON R0.RKLX = CR.BM LEFT JOIN YK_ZDGYS gys ON R0.gysbm = gys.BM   " +
                                "WHERE r0.rkdh= " + rkdh;
                string cmdtext4 = "select  ckdh as 出库单号,CR.MC AS 出库类型,ks.mc as '领用科室/退药厂商'," +
                               "  c0.lyr as 领用人  FROM YK_CKD0 C0 LEFT JOIN YK_CRLX CR ON C0.CKLX = CR.BM LEFT JOIN zd_ks ks ON C0.ksbm = ks.BM   " +
                               "  WHERE C0.ckdh=(select ckdh from YK_RKD0 where rkdh= "+rkdh+" )";
                SqlCommand com1 = new SqlCommand(cmdtext1, cn);
                SqlCommand com2 = new SqlCommand(cmdtext4, cn);
                SqlDataReader dr1 = com1.ExecuteReader();
                
                textBox1.Text = rkdh.ToString();
                if (dr1.Read())
                {

                    comboBox1.Text = dr1["入库类型"].ToString();
                    textBox13.Text = dr1["采购人员"].ToString();
                    dateTimePicker1.Text = dr1["制单日期"].ToString();
                    textBox12.Text = dr1["供应商/退药科室"].ToString();
                    textBox6.Text = dr1["序号"].ToString();
                    textBox7.Text = dr1["录入"].ToString();
                    textBox9.Text = dr1["审核"].ToString();
                    textBox10.Text = dr1["记账"].ToString();
                    textBox8.Text = dr1["备注"].ToString();
                    r0_flag = (int)dr1["状态"];
                    is_new = false;
                    this.dataGridView1.AllowUserToAddRows = false;

                }
                else
                {
                    this.dataGridView1.AllowUserToAddRows = true;
                    is_new = true;
                }
                dr1.Close();
                SqlDataReader dr2 = com2.ExecuteReader();
                if (dr2.Read())
                {
                    textBox11.Text = dr2["出库单号"].ToString();
                    comboBox2.Text = dr2["出库类型"].ToString();
                    textBox15.Text = dr2["领用人"].ToString();
                    textBox14.Text = dr2["领用科室/退药厂商"].ToString();
                }
                dr2.Close();
                string cmdtext2 = "SELECT R1.ypbm 编码,YP.MC AS 名称,YP.GG as 规格,R1.SL as  数量,R1.jj as 进价,R1.jjje as 进价金额," +
                                   "   R1.pj as 批价,R1.pjje as  批价金额,R1.lsj as  零价,R1.ljje as  零价金额,R1.ph as  批号,R1.fph as  发票号,R1.bz as  备注" +
                                   " FROM YK_RKD1 R1 " +
                                   "   LEFT JOIN ZD_YP YP ON R1.YPBM = YP.BM" +
                                    " WHERE R1.RKDH =" + rkdh +
                                    " ORDER BY R1.AUTORKID";
                SqlDataAdapter da = new SqlDataAdapter(cmdtext2, cn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }


            if (!is_new)
            {
                textBox2.Text = dataGridView1.Rows.Count.ToString();
                decimal textBox3_Text = 0;
                decimal textBox4_Text = 0;
                decimal textBox5_Text = 0;
                for (int index = 0; index < dataGridView1.Rows.Count; index++)
                {
                    textBox3_Text = (decimal)dataGridView1[5, index].Value + textBox3_Text;
                    //textBox4_Text = (decimal)dataGridView1[7, index].Value + textBox4_Text;
                    textBox5_Text = (decimal)dataGridView1[9, index].Value + textBox5_Text;
                }
                textBox3.Text = textBox3_Text.ToString();
                textBox4.Text = textBox4_Text.ToString();
                textBox5.Text = textBox5_Text.ToString();

            }
            else
            {

                //入库单号
                int ckdh = 0;
                try
                {

                    string cmdtext3 = "select ckdh from  yk_ckd0  order by ckdh desc ";
                    SqlConnection cn1 = new SqlConnection(SQL.conStr);
                    {
                        cn1.Open();
                        SqlCommand com = new SqlCommand(cmdtext3, cn1);
                        ckdh = (int)com.ExecuteScalar()+1;
                    }
                    textBox11.Text = ckdh.ToString();
                }
                catch 
                {
                    textBox11.Text = "1"; 
                    MessageBox.Show("首次出库");
                }

                this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                this.dataGridView1.ReadOnly = true;
                this.dataGridView1.MultiSelect = false;
                //
            }
            selectRow();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void selectRow()
        {
            if (r0_flag == 19)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;
               
            }
            else if (r0_flag == 17)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = true;
                button6.Enabled = true;
               
            }
            else if (r0_flag == 16)
            {
                button1.Enabled = true;
                button2.Enabled = true;
                button5.Enabled = false;
                button6.Enabled = false;
                
            }
        }

        private void button2_Click(object sender, EventArgs e)//审核
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string cmdtext3 = "   BEGIN TRANSACTION  " +
                              "UPDATE YK_RKD0 SET " +
                              "SHRY = '"+LoginForm.username+"', " +
                              "JZRY = '', " +
                              "FLAG = 17  " +
                              "WHERE RKDH = " +rkdh +
                              "  UPDATE YK_CKD0 SET " +
                              "SHRY = '"+LoginForm.username+"', " +
                              "JZRY = '', " +
                              "FLAG = 17  " +
                              "WHERE CKDH = " + textBox11.Text +
                              "   COMMIT TRANSACTION ";
            SqlCommand cmd = new SqlCommand(cmdtext3, cn);
            cmd.ExecuteNonQuery();
            fresh();
        }

        private void button5_Click(object sender, EventArgs e)//消审
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string cmdtext3 = "   BEGIN TRANSACTION  " +
                              "UPDATE YK_RKD0 SET " +
                              "SHRY = '', " +
                              "JZRY = '', " +
                              "FLAG = 16  " +
                              "WHERE RKDH = " + rkdh +
                              "  UPDATE YK_CKD0 SET " +
                              "SHRY = '', " +
                              "JZRY = '', " +
                              "FLAG = 16  " +
                              "WHERE CKDH = " + textBox11.Text +
                              "   COMMIT TRANSACTION ";
            SqlCommand cmd = new SqlCommand(cmdtext3, cn);
            cmd.ExecuteNonQuery();
            fresh();
        }

        private void button6_Click(object sender, EventArgs e)//记账
        {
            int jzrq = 0;//记账日期
            SqlConnection cn1 = new SqlConnection(SQL.conStr);
            {
                cn1.Open();
                string cmdtext4 = "select  convert(int,(select Convert(DateTime,getdate())))+1";
                SqlCommand cmd = new SqlCommand(cmdtext4, cn1);
                jzrq = (int)cmd.ExecuteScalar();
            }
            SqlConnection cn2 = new SqlConnection(SQL.conStr);
            {
                cn2.Open();
                string cmdtext3 = "BEGIN TRANSACTION  " +
                                  "UPDATE YK_RKD0 SET " +
                                  "JZRY = '"+LoginForm.username+"', " +
                                  "FLAG = 19,  " +
                                  "jzrq =" +jzrq+
                                  "  WHERE RKDH = " + rkdh +
                                  "   UPDATE YK_CKD0 SET " +
                                  "JZRY = '"+LoginForm.username+"', " +
                                  "FLAG = 19,  " +
                                  "jzrq =" + jzrq +
                                  "  WHERE CKDH = " + textBox11.Text +
                                  "   COMMIT TRANSACTION ";
                
                SqlCommand cmd = new SqlCommand(cmdtext3, cn2);
                cmd.ExecuteNonQuery();
            }
            MessageBox.Show("记账成功");
            fresh();
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            Form f = new 药品直入直出录入单编辑(rkdh,r0_flag);
            f.ShowDialog();
            fresh();
            r0_flag = 16;
        }

        private void button1_Click(object sender, EventArgs e)//保存
        {
            r0_flag = 16;
            string dtp1 = dateTimePicker1.Value.ToShortDateString();
            int dpt_1 = 0;
            int rklx = 0;//入库类型
            int cklx = 0;//出库类型
            string cmdtext6 = "select  convert(int,(select Convert(DateTime,'" + dtp1 + "')))+2";
            string cmdtext7 = "select bm from yk_crlx where mc='"+comboBox1.Text.ToString()+"'";
            string cmdtext8 = "select bm from yk_crlx where mc='" + comboBox2.Text.ToString() + "'";
            SqlConnection cn1 = new SqlConnection(SQL.conStr);//入库日期
            {
                cn1.Open();
                SqlCommand com = new SqlCommand(cmdtext6, cn1);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    dpt_1 = (int)read[0];
                }
                read.Close();
                cn1.Close();
            }

            SqlConnection cn2 = new SqlConnection(SQL.conStr);//入库类型
            {
                cn2.Open();
                SqlCommand com = new SqlCommand(cmdtext7, cn2);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    rklx = System.Convert.ToInt32( read[0]);
                }
                read.Close();
                cn2.Close();
            }

            SqlConnection cn6 = new SqlConnection(SQL.conStr);//出库类型
            {
                cn6.Open();
                SqlCommand com = new SqlCommand(cmdtext8, cn6);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    cklx = System.Convert.ToInt32(read[0]);
                }
                read.Close();
                cn2.Close();
            }

            SqlConnection cn5 = new SqlConnection(SQL.conStr);//供应商编码
            {
                cn5.Open();
                string cmdtext5 = "SELECT bm as 编码   from YK_ZDGYS where  mc= '" + textBox12.Text+"'";
                SqlCommand com = new SqlCommand(cmdtext5, cn5);
                SqlDataReader read5 = com.ExecuteReader();
                if (read5.Read())
                {
                    gysbm = read5["编码"].ToString();
                }
                read5.Close();
            }
            SqlConnection cn7 = new SqlConnection(SQL.conStr);//科室编码
            {
                cn7.Open();
                string cmdtext5 = "SELECT bm as 编码   from zd_ks where  mc= '" + textBox13.Text + "'";
                SqlCommand com = new SqlCommand(cmdtext5, cn7);
                SqlDataReader read5 = com.ExecuteReader();
                if (read5.Read())
                {
                    ksbm = read5["编码"].ToString();
                }
                read5.Close();
            }
            if (is_new)
            {
                string cmdtext3 = "    INSERT INTO YK_RKD0(XH,YKBM,RKDH,RQ,SJ,RKLX,JLLX,GYSBM,LRRY,SHRY,JZRY,FLAG,BZ,JZRQ,CGRY,CKDH,SHDH)" +
                                  "  VALUES(" + rxh + ", '402', " + textBox1.Text + ", " + dpt_1 + ", 60914, " + rklx + ", 1, '" + gysbm + "', '" + LoginForm.username + "', '', '', 16, '" + textBox8.Text + "', 0, '" + textBox13.Text + "'," + textBox11.Text.ToString() + ", '') ";
                cmdtext3 = cmdtext3 + "   INSERT INTO YK_CKD0(XH,YKBM,CKDH,RQ,SJ,CKLX,JLLX,KSBM,SHRY,JZRY,FLAG,BZ,JZRQ,SQDH,LYR,lrry)" +
                                  "  VALUES(" + cxh + ", '402', " + textBox11.Text + ", " + dpt_1 + ", 60914, " + cklx + ", 1, '" + ksbm + "', '', '', 16, '" + textBox8.Text + "', 0, '', '" + textBox15.Text + "', '" + LoginForm.username + "') ";
                //update 出库表
                cmdtext3 = cmdtext3 + "    insert into yk_ckd1 (ckdh,ypbm,bzsl,autorkid,sl,dj,cbdj,je,cbje)  " +
                                  "  select  "+textBox11.Text+",ypbm,bzsl,autorkid,sl,lsj,jj,je,jjje from yk_rkd1 where  rkdh="+rkdh+"  order by autorkid    ";
                
                SqlConnection cn3 = new SqlConnection(SQL.conStr);
                {
                    cn3.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn3);
                    com.ExecuteNonQuery();
                }
                
            }
            else 
            {
                string cmdtext3 = "  update YK_RKD0 set RQ=" + dpt_1 + ", RKLX=" + rklx + "  ,GYSBM='" + gysbm + "' ,BZ='" + textBox8.Text + "'  ,CGRY='" + textBox13.Text + "'  where rkdh= "+rkdh;
                cmdtext3 = cmdtext3 + "   update yk_rkd1 set gysbm='"+gysbm+"'  where  rkdh= "+rkdh;
                //update 出库表
                cmdtext3 = cmdtext3 + "    update YK_CKD0 set RQ=" + dpt_1 + ", CKLX=" + cklx + "  ,KSBM='" + ksbm + "' ,BZ='" + textBox8.Text + "'  ,LYR='" + textBox15.Text + "',lrry='" + LoginForm.username + "'  where ckdh= " + textBox11.Text;
                cmdtext3 = cmdtext3 + "   DELETE from YK_CKD1 WHERE CKDH = " + textBox11.Text ;
                cmdtext3 = cmdtext3 + "    insert into yk_ckd1 (ckdh,ypbm,bzsl,autorkid,sl,dj,cbdj,je,cbje)  " +
                                  "  select  " + textBox11.Text + ",ypbm,bzsl,autorkid,sl,lsj,jj,je,jjje from yk_rkd1 where  rkdh=" + rkdh + "  order by autorkid    ";

                SqlConnection cn4 = new SqlConnection(SQL.conStr);
                {
                    cn4.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn4);
                    com.ExecuteNonQuery();
                }
            }
            is_new = false;
            MessageBox.Show("保存成功");
        }

        private void 药品直入直出单录入_Load(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
            dataGridView3.Visible = false;
            dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView2.AllowUserToAddRows = false;
            dataGridView2.MultiSelect = false;       
            dataGridView2.ReadOnly = true;

            try
            {
                
                string cmdtext1 = "select xh from  yk_rkd0  order by xh desc ";
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlCommand com = new SqlCommand(cmdtext1, cn);
                    rxh = (int)com.ExecuteScalar()+1;
                    
                }
                textBox6.Text = rxh.ToString();

            }
            catch  { return; }
            //try
            //{

            //    string cmdtext1 = "select xh from  yk_rkd0  order by xh desc ";
            //    SqlConnection cn = new SqlConnection(SQL.conStr);
            //    {
            //        cn.Open();
            //        SqlCommand com = new SqlCommand(cmdtext1, cn);
            //        rxh = (int)com.ExecuteScalar() + 1;
            //    }

            //}
            //catch (Exception ee) { return; }
            try
            {

                string cmdtext1 = "select xh from  yk_ckd0  order by xh desc ";
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlCommand com = new SqlCommand(cmdtext1, cn);
                    cxh = (int)com.ExecuteScalar() + 1;
                }

            }
            catch  { return; } 
        }

        private void textBox12_KeyPress(object sender, KeyPressEventArgs e)//供应商press
        {
            string cmdtext4 = "select bm 编码,MC AS 名称 from yk_zdgys where dh like '" + textBox12.Text.Trim() + "%' or bm like '" + textBox12.Text.Trim() + "%' or mc like '" + textBox12.Text.Trim() + "%'";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(cmdtext4, cn);
                DataTable dt = new DataTable();
                da1.Fill(dt);
                dataGridView2.DataSource = dt;
                dataGridView2.Visible = true;
                dataGridView2.Columns[0].Width = 60;
                dataGridView2.Columns[1].Width = 150;
            }

        }

        public void select_bm1()
        {
            string cmdtext5 = "SELECT bm as 编码,MC AS 名称  from yk_zdgys where  dh = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or bm = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or mc = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' ";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlCommand com = new SqlCommand(cmdtext5, cn);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    textBox12.Text = read["名称"].ToString();
                    gysbm = read["编码"].ToString();
                }
                read.Close();
            }
            dataGridView2.Visible = false;
        }

        private void dataGridView2_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            select_bm1();
        }

        private void textBox12_MouseClick(object sender, MouseEventArgs e)
        {
            dataGridView2.Visible = false;
        }

        private void textBox14_KeyPress(object sender, KeyPressEventArgs e)//申请药房press
        {
            string cmdtext4 = "select bm 编码,MC AS 名称 from zd_ks where dh like '" + textBox14.Text.Trim() + "%' or bm like '" + textBox14.Text.Trim() + "%' or mc like '" + textBox14.Text.Trim() + "%'";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(cmdtext4, cn);
                DataTable dt = new DataTable();
                da1.Fill(dt);
                dataGridView3.DataSource = dt;
                dataGridView3.Visible = true;
                dataGridView3.Columns[0].Width = 60;
                dataGridView3.Columns[1].Width = 150;
            }

        }

        private void dataGridView3_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            select_bm2();
        }

        public void select_bm2()
        {
            string cmdtext5 = "SELECT bm as 编码,MC AS 名称  from zd_ks where  dh = '" + dataGridView3.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or bm = '" + dataGridView3.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or mc = '" + dataGridView3.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' ";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlCommand com = new SqlCommand(cmdtext5, cn);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    textBox14.Text = read["名称"].ToString();
                    ksbm = read["编码"].ToString();
                }
                read.Close();
            }
            dataGridView3.Visible = false;
        }

        private void textBox14_MouseClick(object sender, MouseEventArgs e)
        {
            dataGridView3.Visible = false;
        }

    }
}